#include "MediaSettingForm.h"
#include "def.h"

MediaSettingForm::MediaSettingForm()
{
	setWindowFlags( windowFlags() & ~Qt::WindowMaximizeButtonHint);
	//setFixedSize(350 ,300);

	createInterface();

	setWindowTitle(tr(TITLE_FORM_SETTINGS));

	connect(m_pbCancel, SIGNAL(clicked()), this, SLOT(reject()));
	connect(m_pbSave, SIGNAL(clicked()), this, SLOT(pbSave_Click()));
	connect(m_chkAutoBitrate, SIGNAL(toggled(bool)), this, SLOT(m_chkAutoBitrate_toggled(bool)));
	connect(m_slFrameRate, SIGNAL(valueChanged(int)), this, SLOT(m_slFrameRate_valueChanged(int)));
	connect(m_slVideoQuality, SIGNAL(valueChanged(int)), this, SLOT(m_slAudioQuality_valueChanged(int)));
	connect(m_radioOriginal, SIGNAL(toggled(bool)), this, SLOT(resolution_toggle(bool)));
	connect(m_radio320x240, SIGNAL(toggled(bool)), this, SLOT(resolution_toggle(bool)));
	connect(m_radio640x480, SIGNAL(toggled(bool)), this, SLOT(resolution_toggle(bool)));
}

MediaSettingForm::~MediaSettingForm()
{

}


void MediaSettingForm::createInterface()
{
	m_vMainLayout = new QVBoxLayout();
	m_audioBox = new QGroupBox(tr(LABEL_VORBIS));

	QGridLayout *m_grdAudio = new QGridLayout();
	m_lblAudioQuality = new QLabel(tr(LABEL_QUALITY));
	m_lblAudioChannels = new QLabel(tr(LABEL_CHANNEL));
	m_lblAudioSample = new QLabel(tr(LABEL_SAMPLE));

	QHBoxLayout *m_hlayChannel = new QHBoxLayout();
	m_rdMono = new QRadioButton(tr(LABEL_CHANNEL_MONO));
	m_rdStereo = new QRadioButton(tr(LABEL_CHANNEL_STEREO));

	m_hlayChannel->addWidget(m_rdMono);
	m_hlayChannel->addWidget(m_rdStereo);

	m_cbSample = new QComboBox();
	QStringList lstSample;
	lstSample << "8000" << "11025" << "22050" << "32000" << "44100" << "48000";
	m_cbSample->addItems(lstSample);
	
	QHBoxLayout *m_audioQualityLayout = new QHBoxLayout();
	m_lblAudioPercentage = new QLabel();
	m_lblAudioPercentage->setNum(0);
	m_lblAudioPercentage->setFixedWidth(40);
	m_slAudioQuality = new QSlider(Qt::Horizontal);
	m_slAudioQuality->setFixedWidth(200);
	m_slAudioQuality->setMinimum(0);
	m_slAudioQuality->setMaximum(100);
	m_slAudioQuality->setPageStep(10);
	m_slAudioQuality->setSingleStep(1);
	connect(m_slAudioQuality, SIGNAL(valueChanged(int)), m_lblAudioPercentage, SLOT(setNum(int)));

	m_audioQualityLayout->addWidget(m_slAudioQuality);
	m_audioQualityLayout->addWidget(m_lblAudioPercentage);
	m_grdAudio->addWidget(m_lblAudioQuality, 0, 0, Qt::AlignRight);
	m_grdAudio->addLayout(m_audioQualityLayout, 0, 1, Qt::AlignLeft);
	m_grdAudio->addWidget(m_lblAudioChannels, 1, 0, Qt::AlignRight);
	m_grdAudio->addLayout(m_hlayChannel, 1, 1, Qt::AlignLeft);
	m_grdAudio->addWidget(m_lblAudioSample, 2, 0, Qt::AlignRight);
	m_grdAudio->addWidget(m_cbSample, 2, 1, Qt::AlignLeft);
#if !defined(_WIN32) && !defined(_WIN64)
	m_grdAudio->setContentsMargins(0, 0 , 0 ,0);
#endif

	m_videoBox = new QGroupBox(tr(LABEL_THEORA));
	QGridLayout *m_grdVideo = new QGridLayout();
	m_lblFrameRates = new QLabel(tr(LABEL_FRAME_RATE));
	m_slFrameRate = new QSlider(Qt::Horizontal);
	m_slFrameRate->setFixedWidth(150);
	m_slFrameRate->setRange(1, 30);
	QHBoxLayout *m_videoQualityLayout = new QHBoxLayout();
	m_lblVideoPercentage = new QLabel();
	m_lblVideoPercentage->setNum(0);
	m_lblVideoPercentage->setFixedWidth(40);
	m_lblVideoQuality = new QLabel(tr(LABEL_QUALITY));
	m_slVideoQuality = new QSlider(Qt::Horizontal);
	m_slVideoQuality->setFixedWidth(200);
	m_slVideoQuality->setMinimum(0);
	m_slVideoQuality->setMaximum(100);
	m_slVideoQuality->setPageStep(10);
	m_slVideoQuality->setSingleStep(1);
	connect(m_slVideoQuality, SIGNAL(valueChanged(int)), m_lblVideoPercentage, SLOT(setNum(int)));
	m_videoQualityLayout->addWidget(m_slVideoQuality);
	m_videoQualityLayout->addWidget(m_lblVideoPercentage);

	QHBoxLayout *m_videoBitRateLayout = new QHBoxLayout();
	m_lblVideoBitRate_kbps = new QLabel();
	m_lblVideoBitRate_kbps->setNum(0);
	m_lblVideoBitRate_kbps->setFixedWidth(40);
	m_lblVideoBitRate = new QLabel(tr(LABEL_BITRATE));
	m_slVideoBitRate = new QSlider(Qt::Horizontal);
	m_slVideoBitRate->setFixedWidth(200);
	m_slVideoBitRate->setMinimum(32);
	m_slVideoBitRate->setMaximum(1024);
	m_slVideoBitRate->setPageStep(100);
	m_slVideoBitRate->setSingleStep(10);
	connect(m_slVideoBitRate, SIGNAL(valueChanged(int)), m_lblVideoBitRate_kbps, SLOT(setNum(int)));
	m_videoBitRateLayout->addWidget(m_slVideoBitRate);
	m_videoBitRateLayout->addWidget(m_lblVideoBitRate_kbps);

	m_chkAutoBitrate = new QCheckBox(tr(LABEL_AUTO_BITRATE));
	m_lblKeyFrameShift = new QLabel(tr(LABEL_KEYFRAME_COUNT));
	m_txtKeyFrameShift = new QLineEdit();
	m_txtKeyFrameShift->setMaxLength(3);
	m_txtKeyFrameShift->setFixedWidth(40);
	m_txtKeyFrameShift->setValidator(new QIntValidator(1, 100, this));
	m_chkDropFrame = new QCheckBox(tr(LABEL_DROP_FRAME));
	m_chkFastMode = new QCheckBox(tr(LABEL_FAST_MODE));
	QHBoxLayout *layoutCheckBox = new QHBoxLayout();
	layoutCheckBox->addWidget(m_txtKeyFrameShift, 0, Qt::AlignLeft);
	layoutCheckBox->addWidget(m_chkDropFrame, 1, Qt::AlignRight);
	layoutCheckBox->addWidget(m_chkFastMode, 2, Qt::AlignCenter);

	m_lblVideoResolution = new QLabel(tr(LABEL_RESOLUTION));
	m_radioOriginal = new QRadioButton(tr(LABEL_ORG_RESOLUTION));
	m_radio320x240 = new QRadioButton(tr("320x240"));
	m_radio640x480 = new QRadioButton(tr("640x480"));
	QHBoxLayout *layoutRadio = new QHBoxLayout();
	layoutRadio->addWidget(m_radioOriginal, 0, Qt::AlignCenter);
	layoutRadio->addWidget(m_radio320x240, 0, Qt::AlignCenter);
	layoutRadio->addWidget(m_radio640x480, 1, Qt::AlignCenter);

	m_grdVideo->addWidget(m_lblVideoQuality,	0, 0, Qt::AlignRight);
	m_grdVideo->addLayout(m_videoQualityLayout, 0, 1, Qt::AlignLeft);
	m_grdVideo->addWidget(m_lblFrameRates,		1, 0, Qt::AlignRight);
	m_grdVideo->addWidget(m_slFrameRate,		1, 1, Qt::AlignLeft);
	m_grdVideo->addWidget(m_lblVideoBitRate,	2, 0, Qt::AlignRight);
	m_grdVideo->addLayout(m_videoBitRateLayout, 2, 1, Qt::AlignLeft);
	m_grdVideo->addWidget(m_chkAutoBitrate,		3, 1, Qt::AlignLeft);
	m_grdVideo->addWidget(m_lblKeyFrameShift,	4, 0, Qt::AlignRight);
	m_grdVideo->addLayout(layoutCheckBox,		4, 1, Qt::AlignLeft);
	m_grdVideo->addWidget(m_lblVideoResolution, 5, 0, Qt::AlignRight);
	m_grdVideo->addLayout(layoutRadio,			5, 1, Qt::AlignLeft);
	//m_grdVideo->addLayout(layoutCheckBox, 6, 1, Qt::AlignLeft);

#if !defined(_WIN32) && !defined(_WIN64)
	m_grdAudio->setContentsMargins(0, 0 , 0 ,0);
#endif

	QHBoxLayout *m_pbLayout = new QHBoxLayout();
	m_pbSave = new QPushButton(tr(BUTTON_SAVE));
	m_pbCancel = new QPushButton(tr(BUTTON_CANCEL));
	m_pbLayout->addWidget(m_pbSave, 0, Qt::AlignRight);
	m_pbLayout->addWidget(m_pbCancel, 0, Qt::AlignLeft);

	m_audioBox->setLayout(m_grdAudio);
	m_videoBox->setLayout(m_grdVideo);

	m_vMainLayout->addWidget(m_audioBox);
	m_vMainLayout->addWidget(m_videoBox);
	m_vMainLayout->addLayout(m_pbLayout);
	setLayout(m_vMainLayout);
}

void MediaSettingForm::pbSave_Click()
{
	int nAudioQuality = m_slAudioQuality->value();
	int nAudioChannels = m_rdMono->isChecked()?1:2;
	int nAudioSample = m_cbSample->currentText().toInt();
	int nVideoQuality = m_slVideoQuality->value();
	int nVideoFrameRates = m_slFrameRate->value();
	int nVideoBitRate = m_slVideoBitRate->value();
	int nAutoBitRate = m_chkAutoBitrate->isChecked() ? 1 : 0;
	int nKFrameShift = m_txtKeyFrameShift->text().toInt();
	int bDropFrame = m_chkDropFrame->checkState() == Qt::Checked ? 1 : 0;
	int bFastMode = m_chkFastMode->checkState() == Qt::Checked ? 1 : 0;;
	int vSize = 1; // 320x240
	if (m_radioOriginal->isChecked())
		vSize = 0; // original
	else if (m_radio640x480->isChecked())
		vSize = 2; // 640x480

	emit onSaveSettings(nAudioQuality, nAudioChannels, nAudioSample, nVideoQuality, nVideoFrameRates, vSize, nVideoBitRate, nAutoBitRate, nKFrameShift, bDropFrame, bFastMode);
}

void MediaSettingForm::m_chkAutoBitrate_toggled(bool checked)
{
	m_slVideoBitRate->setEnabled(!checked);
	if (checked)
		adjustBitRate();
}

void MediaSettingForm::m_slAudioQuality_valueChanged(int value)
{
	if (m_chkAutoBitrate->isChecked())
		adjustBitRate();
}

void MediaSettingForm::m_slFrameRate_valueChanged(int index)
{
	if (m_chkAutoBitrate->isChecked())
		adjustBitRate();
}

void MediaSettingForm::resolution_toggle(bool checked)
{
	if (checked)
		adjustBitRate();
}

int MediaSettingForm::exec(const int audioQuality, const int audioChannels, const int audioSample, const int videoQuality, const int videoFrameRates, const int vSize, const int bitrate, const int autoBR, const int keyframeshift, const int dropframe, const int fastmode)
{
	// audio quality
	m_slAudioQuality->setValue(audioQuality);
	// audio channels
	if (audioChannels == 1)
		m_rdMono->setChecked(true);
	else
		m_rdStereo->setChecked(true);
	// audio sample
	m_cbSample->setCurrentIndex(m_cbSample->findText(QString::number(audioSample)));
	// video quality
	m_slVideoQuality->setValue(videoQuality);
	// video frame rates
	m_slFrameRate->setValue(videoFrameRates);
	// video bit rate
	m_slVideoBitRate->setValue(bitrate);
	// auto bitrate
	m_chkAutoBitrate->setChecked(autoBR != 0);
	// key frame shift
	m_txtKeyFrameShift->setText(QString::number(keyframeshift));
	// drop frame
	m_chkDropFrame->setCheckState(dropframe ? Qt::Checked : Qt::Unchecked);
	// fast mode
	m_chkFastMode->setCheckState(fastmode ? Qt::Checked : Qt::Unchecked);
	// video resolution
	if (0 == vSize)
		m_radioOriginal->setChecked(true);
	else if (2 == vSize)
		m_radio640x480->setChecked(true);
	else
		m_radio320x240->setChecked(true);
	return QDialog::exec();
}

void MediaSettingForm::adjustBitRate()
{
	float nQuality = m_slVideoQuality->value() / 35.0f;
	float nFPS = m_slFrameRate->value() / 10.0f;
	float nScaled = m_radio320x240->isChecked() ? 1.0f : 4.0f;
	float nBitRate = 98.0f * nQuality * nFPS * nScaled;
	int iBitRate = (int)nBitRate;
	if (iBitRate > 1024)
		iBitRate = 1024;
	m_slVideoBitRate->setValue(iBitRate);
}
